package com.yangjun.service.departmentCustomization;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.config.YamlProcessor.MatchStatus;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yangjun.common.DVConstants;
import com.yangjun.entity.departmentCustomizationAnalysis.PreSalesSectionOverViewEntity;
import com.yangjun.utils.DvDateUtils;
@Service
public class PreSalesSectionService extends DepartmentCustomizationCommonService{
	

	
	
	/**
	 * 
	 * @Description 获取下拉框列表数据
	 * @return
	 * @author yangjun
	 * @Date 2017年3月13日 上午9:50:55
	 */
	public Map<String,List<String>> querySectionDropDownList(){
		Map<String,List<String>> resultMap = Maps.newHashMap();
		resultMap.put("section", querySectionNameList());
		resultMap.put("channel", queryChannelList());
		resultMap.put("room", queryRoomList());
		resultMap.put("staff", queryStaffNameList());
		resultMap.put("source", querySourceNameList());
		resultMap.put("keyProduct", queryKeyProductList());
		return resultMap;
	}
	
	/**
	 * @Description 查询月概览
	 * @param groupName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:32:59
	 */
	public PreSalesSectionOverViewEntity queryMonthSummary(String groupName,String beginDate,String endDate){
		String[] groupNameArr = null;
		if(StringUtils.isNotBlank(groupName)){
			groupNameArr = groupName.split(",");
		}
		return preSalesSectionMapper.queryMonthSummary(groupNameArr,beginDate,endDate);
	}
	private String getSectionSortSql(String column,String sort){
		if(StringUtils.isNotBlank(column)){
			return " order by "+column+" "+sort;
		}else{
			return null;
		}
	}
	/**
	 * @Description 查询渠道科室列表
	 * @param groupName
	 * @param channelName
	 * @param roomName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:33:40
	 */
	public List<PreSalesSectionOverViewEntity> queryChannelSalesTable(String groupName,String channelName,String roomName,String column,String sort,String beginDate,String endDate){
		List<PreSalesSectionOverViewEntity> resultList= Lists.newArrayList();
		String[] groupNameArr = null;
		String[] channelNameArr = null;
		String[] roomNameArr = null;
		if(StringUtils.isNotBlank(groupName)){
			groupNameArr = groupName.split(",");
		}
		if(StringUtils.isNotBlank(channelName)){
			channelNameArr = channelName.split(",");
		}
		if(StringUtils.isNotBlank(roomName)){
			roomNameArr = roomName.split(",");
		}
		List<PreSalesSectionOverViewEntity> preSalesSectionOverViewEntityList = preSalesSectionMapper.querySalesTableByChannel(groupNameArr,channelNameArr,roomNameArr,getSectionSortSql(column,sort),beginDate,endDate);
	
		PreSalesSectionOverViewEntity sumPreSalesSectionOverViewEntity = new PreSalesSectionOverViewEntity();
		for(PreSalesSectionOverViewEntity preSalesSectionOverViewEntity : preSalesSectionOverViewEntityList){
			sumPreSalesSectionOverViewEntity.setValidAdviceCnt(sumPreSalesSectionOverViewEntity.getValidAdviceCnt()+preSalesSectionOverViewEntity.getValidAdviceCnt());
			sumPreSalesSectionOverViewEntity.setOrderCnt(sumPreSalesSectionOverViewEntity.getOrderCnt()+preSalesSectionOverViewEntity.getOrderCnt());
			sumPreSalesSectionOverViewEntity.setSalesOrderAmount(sumPreSalesSectionOverViewEntity.getSalesOrderAmount()+preSalesSectionOverViewEntity.getSalesOrderAmount());

		}
		sumPreSalesSectionOverViewEntity.setChannel("合计");
		sumPreSalesSectionOverViewEntity.setSalePct((float)sumPreSalesSectionOverViewEntity.getSalesOrderAmount()/sumPreSalesSectionOverViewEntity.getOrderCnt());
		sumPreSalesSectionOverViewEntity.setSalesRate((float)sumPreSalesSectionOverViewEntity.getSalesOrderAmount()/sumPreSalesSectionOverViewEntity.getSalesOrderAmount());

		resultList.add(sumPreSalesSectionOverViewEntity);
		resultList.addAll(preSalesSectionOverViewEntityList);
		
		return resultList;
	}
	public List<PreSalesSectionOverViewEntity> queryPlatformSalesTable(String groupName,String channelName,String roomName,String column,String sort,String beginDate,String endDate){
		List<PreSalesSectionOverViewEntity> resultList= Lists.newArrayList();
		String[] groupNameArr = null;
		String[] channelNameArr = null;
		String[] roomNameArr = null;
		if(StringUtils.isNotBlank(groupName)){
			groupNameArr = groupName.split(",");
		}
		if(StringUtils.isNotBlank(channelName)){
			channelNameArr = channelName.split(",");
		}
		if(StringUtils.isNotBlank(roomName)){
			roomNameArr = roomName.split(",");
		}
		List<PreSalesSectionOverViewEntity> preSalesSectionOverViewEntityList = preSalesSectionMapper.querySalesTableByRoom(groupNameArr,channelNameArr,roomNameArr,getSectionSortSql(column,sort),beginDate,endDate);
		PreSalesSectionOverViewEntity sumPreSalesSectionOverViewEntity = new PreSalesSectionOverViewEntity();
		for(PreSalesSectionOverViewEntity preSalesSectionOverViewEntity : preSalesSectionOverViewEntityList){
			sumPreSalesSectionOverViewEntity.setValidAdviceCnt(sumPreSalesSectionOverViewEntity.getValidAdviceCnt()+preSalesSectionOverViewEntity.getValidAdviceCnt());
			sumPreSalesSectionOverViewEntity.setOrderCnt(sumPreSalesSectionOverViewEntity.getOrderCnt()+preSalesSectionOverViewEntity.getOrderCnt());
			sumPreSalesSectionOverViewEntity.setSalesOrderAmount(sumPreSalesSectionOverViewEntity.getSalesOrderAmount()+preSalesSectionOverViewEntity.getSalesOrderAmount());

		}
		sumPreSalesSectionOverViewEntity.setSourceName("合计");
		sumPreSalesSectionOverViewEntity.setSalePct((float)sumPreSalesSectionOverViewEntity.getSalesOrderAmount()/sumPreSalesSectionOverViewEntity.getOrderCnt());
		sumPreSalesSectionOverViewEntity.setSalesRate((float)sumPreSalesSectionOverViewEntity.getSalesOrderAmount()/sumPreSalesSectionOverViewEntity.getSalesOrderAmount());

		resultList.add(sumPreSalesSectionOverViewEntity);
		resultList.addAll(preSalesSectionOverViewEntityList);
		
		return resultList;
	}

	
	/**
	 * @Description 查询趋势对比时数据
	 * @param contrastDate
	 * @param groupName
	 * @param channelName
	 * @param roomName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:34:28
	 */
	public Map<String,Object> querySectionComparisonLineChartByHourofDay(String contrastDate,String groupName,String channelName,String roomName){
		System.out.println(contrastDate+"====="+groupName+"====="+channelName+"====="+roomName);
		String[] groupNameArr = null;
		String[] channelNameArr = null;
		String[] roomNameArr = null;
		if(StringUtils.isNotBlank(groupName)){
			groupNameArr = groupName.split(",");
		}
		if(StringUtils.isNotBlank(channelName)){
			channelNameArr = channelName.split(",");
		}
		if(StringUtils.isNotBlank(roomName)){
			roomNameArr = roomName.split(",");
		}
		int index = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		String currentDate = sdfDay.format(new Date());
		String[] dateArr = {currentDate,contrastDate};
		System.out.println(Arrays.toString(groupNameArr));
		System.out.println(Arrays.toString(channelNameArr));
		System.out.println(Arrays.toString(roomNameArr));
		System.out.println(Arrays.toString(dateArr));

		//一次性查询两天的数据
		List<PreSalesSectionOverViewEntity> todayLineChartByHourList = preSalesSectionMapper.querySectionComparisonLineChartByHour(groupNameArr,channelNameArr,roomNameArr,dateArr);
		return queryLineChartData(DVConstants.HOUR_ARR,todayLineChartByHourList,dateArr,DVConstants.COMPARISON_LEGEND,index);
	}
	public Map<String,Object> querySectionComparisonLineChartByDayofWeek(String groupName,String channelName,String roomName){
		String[] groupNameArr = null;
		String[] channelNameArr = null;
		String[] roomNameArr = null;
		if(StringUtils.isNotBlank(groupName)){
			groupNameArr = groupName.split(",");
		}
		if(StringUtils.isNotBlank(channelName)){
			channelNameArr = channelName.split(",");
		}
		if(StringUtils.isNotBlank(roomName)){
			roomNameArr = roomName.split(",");
		}
		String currentDate = sdfDay.format(new Date());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.WEEK_OF_MONTH, -1);
		String contrastDate = sdfDay.format(calendar.getTime());
		String[] dateArr = {currentDate,contrastDate};
		//一次性查询两天的数据
		List<PreSalesSectionOverViewEntity> todayLineChartByHourList = preSalesSectionMapper.querySectionComparisonLineChartByHour(groupNameArr,channelNameArr,roomNameArr,dateArr);
		int index = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		return queryLineChartData(DVConstants.HOUR_ARR,todayLineChartByHourList,dateArr,DVConstants.COMPARISON_LEGEND,index);
	}
	/**
	 * @Description 趋势对比日月对比
	 * @param groupName
	 * @param channelName
	 * @param roomName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:42:01
	 */
	public Map<String,Object> querySectionComparisonLineChartByDayofMonth(String groupName,String channelName,String roomName){
		String[] groupNameArr = null;
		String[] channelNameArr = null;
		String[] roomNameArr = null;
		if(StringUtils.isNotBlank(groupName)){
			groupNameArr = groupName.split(",");
		}
		if(StringUtils.isNotBlank(channelName)){
			channelNameArr = channelName.split(",");
		}
		if(StringUtils.isNotBlank(roomName)){
			roomNameArr = roomName.split(",");
		}
		String currentDate = sdfMonth.format(new Date());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.MONTH, -1);
		String contrastDate = sdfMonth.format(calendar.getTime());
		String[] dateArr = {currentDate,contrastDate};
		//一次性查询两天的数据
		List<PreSalesSectionOverViewEntity> todayLineChartByHourList = preSalesSectionMapper.querySectionComparisonLineChartByDay(groupNameArr,channelNameArr,roomNameArr,dateArr);
		int index = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
		return queryLineChartData(DVConstants.MONTH_DAY,todayLineChartByHourList,dateArr,DVConstants.COMPARISON_LEGEND,index);
	}
	
	/**
	 * @Description 趋势对比日和周对比折线图
	 * @param groupName
	 * @param channelName
	 * @param roomName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:41:34
	 */
	public Map<String,Object> querySectionComparisonLineChartByWeek(String groupName,String channelName,String roomName){
		String[] groupNameArr = null;
		String[] channelNameArr = null;
		String[] roomNameArr = null;
		if(StringUtils.isNotBlank(groupName)){
			groupNameArr = groupName.split(",");
		}
		if(StringUtils.isNotBlank(channelName)){
			channelNameArr = channelName.split(",");
		}
		if(StringUtils.isNotBlank(roomName)){
			roomNameArr = roomName.split(",");
		}
		Calendar calendar = Calendar.getInstance();
		String currentDate = sdfDay.format(new Date());
		calendar.setTime(new Date());
		int dayOfWeek= calendar.get(Calendar.DAY_OF_WEEK)-1;
		calendar.add(Calendar.DAY_OF_MONTH, -(dayOfWeek+6));
		String contrastDate = sdfDay.format(calendar.getTime());
		calendar.add(Calendar.DAY_OF_MONTH, +6);
		Date tmpDate = calendar.getTime();
		//一次性查询两天的数据
		List<PreSalesSectionOverViewEntity> todayLineChartByHourList = setDayOfWeek(preSalesSectionMapper.querySectionComparisonLineChartByWeek(groupNameArr,channelNameArr,roomNameArr,currentDate,contrastDate),tmpDate);
		int index = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)-1;
		return queryLineChartData(DVConstants.WEEK_NAME_ARR,todayLineChartByHourList,new String[]{"本周","上周"},DVConstants.COMPARISON_LEGEND,index);
	}
	
	
	
	
	
	/**
	 * @Description 查询表格数据（近30天）
	 * @param groupName
	 * @param channelName
	 * @param roomName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:37:45
	 */
	public Map<String,List<String[]>> querySectionTableDataByDay(String groupName,String channelName,String roomName){
		String[] groupNameArr = null;
		String[] channelNameArr = null;
		String[] roomNameArr = null;
		if(StringUtils.isNotBlank(groupName)){
			groupNameArr = groupName.split(",");
		}
		if(StringUtils.isNotBlank(channelName)){
			channelNameArr = channelName.split(",");
		}
		if(StringUtils.isNotBlank(roomName)){
			roomNameArr = roomName.split(",");
		}
		Date today = new Date();
		String endDate = sdfDay.format(today);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(today);
		calendar.add(Calendar.MONTH, -1);
		String beginDate = sdfDay.format(calendar.getTime());
		//一次性查询两天的数据
		List<PreSalesSectionOverViewEntity> tableByDayList = preSalesSectionMapper.querySectionTableData(groupNameArr,channelNameArr,roomNameArr,beginDate,endDate,null);
		return queryTableData(tableByDayList,DVConstants.HOUR_ARR,"day");
	}
	/**
	 * 
	 * @Description （近12个月）
	 * @param groupName
	 * @param channelName
	 * @param roomName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:38:13
	 */
	public Map<String,List<String[]>> querySectionTableDataByMonth(String groupName,String channelName,String roomName){
		String[] groupNameArr = null;
		String[] channelNameArr = null;
		String[] roomNameArr = null;
		if(StringUtils.isNotBlank(groupName)){
			groupNameArr = groupName.split(",");
		}
		if(StringUtils.isNotBlank(channelName)){
			channelNameArr = channelName.split(",");
		}
		if(StringUtils.isNotBlank(roomName)){
			roomNameArr = roomName.split(",");
		}
		Date today = new Date();
		String endDate = sdfDay.format(today);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(today);
		calendar.add(Calendar.YEAR, -1);
		calendar.set(Calendar.DAY_OF_MONTH,1);
		String beginDate = sdfDay.format(calendar.getTime());
		//一次性查询两天的数据
		List<PreSalesSectionOverViewEntity> tableByDayList = preSalesSectionMapper.querySectionTableData(groupNameArr,channelNameArr,roomNameArr,beginDate,endDate,"MONTH");
		return queryTableData(tableByDayList,DVConstants.MONTH_DAY,"month");
	}
	/**
	 * @Description （近1年）
	 * @param groupName
	 * @param channelName
	 * @param roomName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:38:31
	 */
	public Map<String,List<String[]>> querySectionTableDataByYear(String groupName,String channelName,String roomName){
		String[] groupNameArr = null;
		String[] channelNameArr = null;
		String[] roomNameArr = null;
		if(StringUtils.isNotBlank(groupName)){
			groupNameArr = groupName.split(",");
		}
		if(StringUtils.isNotBlank(channelName)){
			channelNameArr = channelName.split(",");
		}
		if(StringUtils.isNotBlank(roomName)){
			roomNameArr = roomName.split(",");
		}
		Date today = new Date();
		String endDate = sdfDay.format(today);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(today);
		calendar.add(Calendar.YEAR, -1);
		calendar.set(Calendar.DAY_OF_YEAR,1);
		String beginDate = sdfDay.format(calendar.getTime());
		//一次性查询两天的数据
		List<PreSalesSectionOverViewEntity> tableByDayList = preSalesSectionMapper.querySectionTableData(groupNameArr,channelNameArr,roomNameArr,beginDate,endDate,"YEAR");
		return queryTableData(tableByDayList,DVConstants.MONTH,"year");
	}
}